package com.haruhinav.service.impl;

import com.haruhinav.entity.MusicServer;
import com.haruhinav.entity.PlayList;
import com.haruhinav.entity.UserPlaylist;
import com.haruhinav.mapper.MusicMapper;
import com.haruhinav.service.MusicService;
import com.haruhinav.Utils.TimeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MusicServiceImpl implements MusicService {

    @Resource
    private MusicMapper musicServerMapper;
    @Autowired
    private TimeTool timeTool;
    //    所有音乐平台
    @Override
    public List<MusicServer> getAllMusicServer() {
        return musicServerMapper.getAllMusicServer();
    }

    //所有歌单  一对多(歌单整理功能)
    @Override
    public Map<String,Object> getAllPlayListBySId() {

        Map<String,Object> msMap= new HashMap<>();
        List<MusicServer> msList = musicServerMapper.getAllPlayListBySId();
        int cont=0;
        for (MusicServer ms:msList){
            cont+=ms.getPlayList().size();
            ms.getPlayList().forEach(pl->pl.setCreateTime(timeTool.SubTime(pl.getCreateTime())));
        }
        msMap.put("plCont",cont);//所有平台歌单总数
        msMap.put("msList",msList);
        return msMap;
    }

    @Override
    public List<MusicServer> findMusicServerByUserNotInSId(int uid) {
        return musicServerMapper.findMusicServerByUserNotInSId(uid);
    }

    @Override
    public List<MusicServer> findPlaylistByUser(int uid) {

        List<MusicServer> ms = musicServerMapper.findPlaylistByUser(uid);
        for(MusicServer m:ms){
            m.getUserPlaylistList().forEach(item->item.setCreateTime(timeTool.SubTime(item.getCreateTime())));
        }
        return ms;
    }

    @Override
    public List<PlayList> findPlaylistByExplainAndSId(PlayList playList) {
        List<PlayList> pls = musicServerMapper.findPlaylistByExplainAndSId(playList);
        pls.forEach(pl->pl.setCreateTime(timeTool.SubTime(pl.getCreateTime())));
        return pls;
    }

    @Override
    public List<PlayList> findPlaylistByExplainAndSId2(String exp, Integer sid) {
        return musicServerMapper.findPlaylistByExplainAndSId2(exp,sid);
    }

    @Override
    public Map<String,Object> insertUserPlaylist(UserPlaylist userPlaylist) {
        Map<String,Object> insert_flagMap = new HashMap<>();

//        UserPlaylist userPlaylistEX =userPlaylist;//用作查询条件
//        userPlaylistEX.setPlExplain(null);//将备注设为null(不做查询条件)
        //根据条件查找用户歌单
         UserPlaylist findUserPlaylist = musicServerMapper.findplByUserPlaylist(
                 userPlaylist.getUserId(),userPlaylist.getServerId(),userPlaylist.getPlaylistId(),null);

         if(findUserPlaylist!=null){
//             若查出数据，则不能添加
             insert_flagMap.put("itEx",1);
             insert_flagMap.put("oldId",findUserPlaylist.getId());
             return insert_flagMap;
         }
        musicServerMapper.insertUserPlaylist(userPlaylist);
        insert_flagMap.put("itEx",0);
        insert_flagMap.put("newId",userPlaylist.getId());

        return insert_flagMap;
    }

    @Override
    public int removePlaylistById(int id) {
        return musicServerMapper.removePlaylistById(id);
    }

    @Override
    public int removePlaylistByUIdAndSid(int uid, int sid) {
        return musicServerMapper.removePlaylistByUIdAndSid(uid,sid);
    }

    @Override
    public UserPlaylist findUserPlaylistById(int id) {
        return musicServerMapper.findUserPlaylistById(id);
    }

    @Override
    public Map<String,Object> updateUserPlaylistById(UserPlaylist userPlaylist) {
        Map<String,Object> update_flagMap = new HashMap<>();
        //根据条件查找用户歌单
        UserPlaylist findUserPlaylist = musicServerMapper.findplByUserPlaylist(
                userPlaylist.getUserId(),userPlaylist.getServerId(),userPlaylist.getPlaylistId(),null);
        if(findUserPlaylist!=null && findUserPlaylist.getId()!=userPlaylist.getId()){
//             若查出数据，则不能修改
            update_flagMap.put("itEx",1);
            update_flagMap.put("Updates",0);
            update_flagMap.put("findId",findUserPlaylist.getId());
            return update_flagMap;
        }
        int Updates= musicServerMapper.updateUserPlaylistById(userPlaylist);//执行修改
        update_flagMap.put("itEx",0);
        update_flagMap.put("Updates",Updates);
        update_flagMap.put("updateId",userPlaylist.getId());
        return update_flagMap;
    }

    @Override
    public int updateUPlExplainById(String explain, int id) {
        return musicServerMapper.updateUPlExplainById(explain,id);
    }
}
